module Database;

import DND;
import Node;
import DefVar;

void Database::_load_db()
{
	if (true)
	{
		//utf8临时目录
		g_file->CreateDirectory("data/config/u8/");
		//转换编码
		g_file->ConvertMb2U8("data/config/terrain.csv", "data/config/u8/terrain.csv");
		g_file->ConvertMb2U8("data/config/age.csv", "data/config/u8/age.csv");
		g_file->ConvertMb2U8("data/config/location.csv", "data/config/u8/location.csv");
		//序列化
		g_file->SerializeCsv("data/config/u8/terrain.csv", "data/bin/config/terrain.dat");
		g_file->SerializeCsv("data/config/u8/age.csv", "data/bin/config/age.dat");
		g_file->SerializeCsv("data/config/u8/location.csv", "data/bin/config/location.dat");
	}
	

	vector<SLTerrain> db;
	g_file->DeserializeCsv("data/bin/config/terrain.dat", db);

	for (auto& iter : db)
	{
		DbTerrain& t = _dbTerrain[iter._id];
		t._id = iter._id;
		t._name = iter._name;

		_mapName2Id[iter._name] = iter._id;
	}

	//age
	vector<SLAge> all_age;
	g_file->DeserializeCsv("data/bin/config/age.dat", all_age);

	for_couple(all_age, [&](const SLAge& a, const SLAge& b)
		{
			DbAge age;
			age._range = { a._age, b._age };
			age._name = a._name;
			_dbAge.push_back(age);
		});

	//location
	vector<SLLocation> all_location;
	g_file->DeserializeCsv("data/bin/config/location.dat", all_location);

	for (auto& iter : all_location)
	{
		DbLocation location;
		location._name = iter._name;
		Point ll = toPoint(iter._xy * (float)DefVar::NUM_NODE_PER_L);
		location._xy = Ll2Xy(ll);
		_dbLocation.push_back(location);
	}
	int i = 3;
}
